package com.downhometech.productcalculator.activities;

import java.util.ArrayList;

import com.downhometech.productcalculator.controllers.ProductController;
import com.downhometech.productcalculator.models.Cart;
import com.downhometech.productcalculator.models.Product;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ItemListActivity extends  DefaultActivity {

	private ArrayList<Product> productList = new ArrayList<Product>();
	private ProductController controller;
	
	private class EfficientAdapter extends BaseAdapter {
		private LayoutInflater mInflater;
		private Context parentContext;
		
		
		public EfficientAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
			parentContext = context;
		}

		public int getCount() {
			return productList.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) 
		{
			ViewHolder holder;
			Product product = productList.get(position);
			if (convertView == null) 
			{
				convertView = mInflater.inflate(R.layout.itemlist_item, null);
				
				holder = new ViewHolder();
				holder.itemName = (TextView) convertView.findViewById(R.id.ItemList_Name);
				holder.itemImage = (ImageView) convertView.findViewById(R.id.ItemList_Image);
				holder.item = (RelativeLayout) convertView.findViewById(R.id.ItemList_Layout);
				convertView.setTag(holder);
			}
			else 
			{
				holder = (ViewHolder) convertView.getTag();
			}
			
			
			holder.itemName.setText(product.getName());
			
			if (product.isCategory()) { holder.itemImage.setImageResource(R.drawable.category_add); }
			else { holder.itemImage.setVisibility(ImageView.INVISIBLE); }

			return convertView;
		}


		class ViewHolder 
		{
			TextView itemName;
			int productId;
			ImageView itemImage;
			RelativeLayout item;		
		}
		
		class ViewClickListener implements OnClickListener 
		{
			private Product product;
			public ViewClickListener(Product product)
	        {
	            this.product = product;
	        }

			public void onClick(View view) 
			{
				Intent intent = null;
				if (product.isCategory() && false) 
				{
					intent = new Intent(parentContext, ItemListActivity.class);
					intent.putExtra("_id", product.getId());
				}
				else
				{
					intent = new Intent(parentContext, ProductDescriptionActivity.class);
					intent.putExtra("_productId", product.getId());
				}
				
				startActivity(intent);
			}
		}
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		Bundle passedInBundle = this.getIntent().getExtras();
		ProductCalculator application = ((ProductCalculator)getApplicationContext());
		
		int id = passedInBundle.getInt("_id");
		boolean useAutoComplete = passedInBundle.getBoolean("_useAutoComplete");
		
		setContentView(R.layout.itemlist);
		
		controller = new ProductController(this);		
		final ListView itemList = (ListView) findViewById(R.id.ItemList_List);
		AutoCompleteTextView  searchField = (AutoCompleteTextView ) findViewById(R.id.ItemList_Search);
		ArrayAdapter<Product> autoCompleteAdapter;
		
		if (useAutoComplete)
		{
			productList = controller.getAllProducts();
			itemList.setVisibility(ListView.INVISIBLE);
			autoCompleteAdapter = new ArrayAdapter<Product>(this, R.layout.itemlist_searchresults, productList);
			searchField.setAdapter(autoCompleteAdapter);
			searchField.setOnItemClickListener(new OnItemClickListener() 
			{
				public void onItemClick(AdapterView<?> adapter, View view, int adapterLocation, long arg3) 
				{
					Product product = (Product)adapter.getItemAtPosition(adapterLocation);
					Intent intent = new Intent(ItemListActivity.this, ProductDescriptionActivity.class);
					intent.putExtra("_productId", new Integer(product.getId()));
					startActivity(intent);				
				}
			});			
		}
		else
		{
			if (id < 0 ) { productList = controller.getAllProducts();  }
			else { productList = controller.getAllProductsForCategory(id); }
			searchField.setVisibility(AutoCompleteTextView.INVISIBLE);
			autoCompleteAdapter = new ArrayAdapter<Product>(this, R.layout.itemlist_searchresults, productList);
			//itemList.setAdapter(autoCompleteAdapter);
			itemList.setAdapter(new EfficientAdapter(this));
			itemList.setOnItemClickListener(new OnItemClickListener() 
			{
				 @Override
				 public void onItemClick(AdapterView<?> a, View v, int position, long id) 
				 {
					Product product = productList.get(position);					 
					Intent intent = null;
					if (product.isCategory()) 
					{
						intent = new Intent(ItemListActivity.this, ItemListActivity.class);
						intent.putExtra("_id", product.getId());
					}
					else
					{
						intent = new Intent(ItemListActivity.this, ProductDescriptionActivity.class);
						intent.putExtra("_productId", product.getId());
					}
					
					startActivity(intent);
					 
				 }
			 });
		}
	}
}